package com.showsu.editor.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.showsu.editor.dao.TblSysLogsMapper;
import com.showsu.editor.entity.po.TblSysLog;
import com.showsu.editor.service.ISysLogService;
import com.showsu.editor.utils.OsAndBrowserUtil;
import com.showsu.editor.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: eollse-wj
 * @Date: 2019-12-20 16:19:04
 * @Description:
 */
@Service
public class SysLogServiceImpl extends ServiceImpl<TblSysLogsMapper, TblSysLog> implements ISysLogService {

    @Autowired
    private TblSysLogsMapper tblSysLogsMapper;

    @Override
    public int add(HttpServletRequest request, String action, String msg, Long userId, String name) {
        String clientOrigin = "99";
        String ip = null;
        String os = null;
        String browser = null;
        if (request != null) {
            OsAndBrowserUtil osAndBrowser = OsAndBrowserUtil.getOSAndBrowser(request);
            if (!StringUtil.isEmpty(request.getHeader("clientOrigin"))){
                clientOrigin = request.getHeader("clientOrigin");
            }
            ip = osAndBrowser.getIP();
            os = osAndBrowser.getOs();
            browser = osAndBrowser.getBrowser();
        }
        TblSysLog logs = new TblSysLog(action, msg,ip, os, browser, clientOrigin, userId,name);
        return tblSysLogsMapper.insert(logs);
    }
}